﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SQLServerHealthCheck.Model;
using System.Data.SqlClient;
using AG.Utilities;

namespace SQLServerHealthCheck.SQLServer
{
    public delegate List<Exception> PerformDatabackup(String backupName);

    public  class BackupRestore
    {

        public static List<Exception> BackupDatabase(String backupName)
        {

            List<Exception> errors = new List<Exception>();
            try
            {
                errors = new List<Exception>();

                String conString =System.Configuration.ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"].ToString();



                if (CollectServerAndServiceData.IsSQLServerAccessible(conString))
                {
                    using (SqlConnection cn = new SqlConnection(conString))
                    {
                        cn.Open();

                        
                        String sql = String.Format(@"
                        BACKUP DATABASE [{0}] 
	                        TO DISK = N'{1}' 
                        WITH 
	                        FORMAT,  NAME = N'[SQLServerHealthCheck_backup_{1}]'

                        ", cn.Database, backupName);

                        SqlCommand cmd = new SqlCommand(sql, cn);

                        cmd.ExecuteNonQuery();

                    }
                }

            }
            catch (Exception ex1)
            {
                errors.Add(ex1);
                Logger.Error(ex1);
            }

            return errors;
        }

       

        public static List<Exception> RestoreDatabase(String backupName)
        {

            List<Exception> errors = new List<Exception>();
            try
            {
                errors = new List<Exception>();

                String conString = System.Configuration.ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"].ToString();
                
                if (CollectServerAndServiceData.IsSQLServerAccessible(conString))
                {
                    using (SqlConnection cn = new SqlConnection(conString))
                    {
                        cn.Open();


                        String sql = String.Format(@"

                        USE MASTER
                        ALTER DATABASE [{0}] 
	                        SET  SINGLE_USER WITH ROLLBACK IMMEDIATE

                        RESTORE DATABASE [{0}] 
                        FROM  DISK = '{1}' WITH  FILE = 1,  REPLACE
                        ALTER DATABASE [{0}] 
	                        SET  SINGLE_USER WITH ROLLBACK IMMEDIATE


                           ALTER DATABASE [{0}] 
                            SET MULTI_USER;
                        ", cn.Database, backupName);

                                                
                        

                        SqlCommand cmd = new SqlCommand(sql, cn);

                        cmd.ExecuteNonQuery();

                    }
                }

            }
            catch (System.Security.SecurityException ex1)
            {
                errors.Add(ex1);
                Logger.Error(ex1);
            }

            return errors;
        }


    }
}
